package com.zd.web.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.zd.entity.Novel;
import com.zd.entity.NovelAndCollection;
import com.zd.entity.User;
import com.zd.service.MessageService;
import com.zd.service.impl.MessageServiceImpl;

/**
 * Servlet implementation class MessageServlet
 */
@WebServlet("/MessageServlet")
public class MessageServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
   
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("UTF-8");
		String mytext=request.getParameter("mytext");
		request.setAttribute("mytext", mytext);
		MessageService messageservice=new MessageServiceImpl();
		User user=(User)request.getSession().getAttribute("user");
		if(user==null) {
			response.sendRedirect("login.jsp");
			return;
		}
		List<Novel> list=messageservice.select(mytext);
		List<Integer> list1=messageservice.yes(user.getUserId());
		List<NovelAndCollection> novelAndCollections = new ArrayList<>();
		//遍历搜索出来的list对象,将其数据更改到新的集合对象中
		for(int i = 0;i<list.size();i++) {
			Novel novle = list.get(i);
			NovelAndCollection novelAndCollection =  new NovelAndCollection();
			novelAndCollection.setHits(novle.getHits());
			novelAndCollection.setNovelId(novle.getNovelId());
			novelAndCollection.setNovelImgURL(novle.getNovelImgURL());
			novelAndCollection.setNovelIntroduction(novle.getNovelIntroduction());
			novelAndCollection.setWriterName(novle.getWriterName());
			novelAndCollection.setNovelName(novle.getNovelName());
			novelAndCollection.setReleaseTime(novle.getReleaseTime());
			novelAndCollection.setNovelType1(novle.getNovelType1());
			novelAndCollections.add(novelAndCollection);
		}
		//遍历novelAndCollection集合,如果其中书籍id相等,则视为已借出
		for(int i = 0;i<novelAndCollections.size();i++) {
			for(int j = 0;j<list1.size();j++) {
				//获取每一个novelAndCollection对象
				NovelAndCollection novelAndCollection = novelAndCollections.get(i);
				//比较每一个对象与书籍的id值
				if(novelAndCollection.getNovelId() == list1.get(j)) {
					//如果相等,则视为已收藏
					novelAndCollection.setIsCollection(1);
					break;
				}
			
			}
		}
		request.setAttribute("novelid", novelAndCollections);
//		for (int i = 0; i < list.size(); i++) {
//			for (int j = i; j < list1.size(); j++) {
//				if(list.get(i).getNovelId()==list1.get(j));
//					int novelid=list1.get(j);
//					request.setAttribute("novelid", novelid);
//			}
//		}
//		request.setAttribute("list", list);
		request.getRequestDispatcher("sousuo.jsp").forward(request, response);
		
	}
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
	}

}
